package mock;

import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Matchers;
import org.mockito.Mockito;

import service.TokenStore;
import servlets.AddTeilnehmerServlet;

public class AddTeilnehmerServletTest {
	
	HttpServletRequest mockRequest;
	HttpServletResponse mockResponse;
	HttpSession mockSession;
	RequestDispatcher mockRequestDispatcher;
	
	AddTeilnehmerServlet servlet;
	
	@Before
	public void setup() {
		
		mockRequest = Mockito.mock(HttpServletRequest.class);
		mockResponse = Mockito.mock(HttpServletResponse.class);
		mockSession = Mockito.mock(HttpSession.class);
		when(mockRequest.getSession()).thenReturn(mockSession);
		mockRequestDispatcher = Mockito.mock(RequestDispatcher.class);
		when(mockRequest.getRequestDispatcher(Matchers.anyString())).thenReturn(mockRequestDispatcher);
		
		servlet = new AddTeilnehmerServlet();
	}

	@Test
	public void doGet() throws ServletException, IOException {
		
//	  when(requestMock.getAttribute("email")).thenReturn("xyz@abc.com");
	  
		servlet.doGet(mockRequest, mockResponse);
		
		verify(mockSession, times(1)).setAttribute(Matchers.matches("token"), Matchers.any(Object.class));
		verify(mockRequest, times(1)).setAttribute(Matchers.matches("TerminId"), Matchers.any(Object.class));
		verify(mockRequest, times(1)).getRequestDispatcher(Matchers.matches("/WEB-INF/jsps/addTeilnehmer.jsp"));
		verify(mockRequestDispatcher, times(1)).forward(Matchers.any(HttpServletRequest.class), Matchers.any(HttpServletResponse.class));
	}
	
	 @Test
	  public void doPostWithInvalidToken() throws ServletException, IOException {
	    
	    when(mockRequest.getParameter(Matchers.matches("token"))).thenReturn("123");
	    when(mockRequest.getParameter(Matchers.matches("TerminId"))).thenReturn("1");
	    when(mockRequest.getParameter(Matchers.matches("TeilnehmerId"))).thenReturn("1");
	    when(mockRequest.getParameter(Matchers.matches("anrede"))).thenReturn("Herr");
	    when(mockRequest.getParameter(Matchers.matches("vorname"))).thenReturn("Detlef");
	    when(mockRequest.getParameter(Matchers.matches("nachname"))).thenReturn("Knut");
	    
	    servlet.doPost(mockRequest, mockResponse);
	    
	    verify(mockRequest, times(1)).getRequestDispatcher(Matchers.matches("/WEB-INF/jsps/error.jsp"));
	  }
	  
	  @Test
	  public void doPostWithInvalidParameters() throws ServletException, IOException {
	    
	    String token = TokenStore.getInstance().createToken();
	    when(mockRequest.getParameter(Matchers.matches("token"))).thenReturn(token);
	    when(mockRequest.getParameter(Matchers.matches("TerminId"))).thenReturn("1");
	    when(mockRequest.getParameter(Matchers.matches("TeilnehmerId"))).thenReturn("1");
	    when(mockRequest.getParameter(Matchers.matches("anrede"))).thenReturn(null);
	    when(mockRequest.getParameter(Matchers.matches("vorname"))).thenReturn("Detlef");
	    when(mockRequest.getParameter(Matchers.matches("nachname"))).thenReturn("Knut");
	    
	    servlet.doPost(mockRequest, mockResponse);
	    
      verify(mockRequest, times(1)).getRequestDispatcher(Matchers.matches("/WEB-INF/jsps/error.jsp"));
	  }
	
	@Test
	public void doPostWithValidToken() throws ServletException, IOException {
		

		String token = TokenStore.getInstance().createToken();
		when(mockRequest.getParameter(Matchers.matches("token"))).thenReturn(token);
    when(mockRequest.getParameter(Matchers.matches("TerminId"))).thenReturn("1");
    when(mockRequest.getParameter(Matchers.matches("TeilnehmerId"))).thenReturn("1");
    when(mockRequest.getParameter(Matchers.matches("anrede"))).thenReturn("Herr");
    when(mockRequest.getParameter(Matchers.matches("vorname"))).thenReturn("Detlef");
    when(mockRequest.getParameter(Matchers.matches("nachname"))).thenReturn("Knut");
		
		servlet.doPost(mockRequest, mockResponse);
		
		verify(mockResponse, times(1)).sendRedirect(Matchers.matches("TerminServlet?TerminId=1"));
	}
	
	

}
